package top.hzy520.stock.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.stock.entity.ReduceStock;
import top.hzy520.stock.entity.Stock;
import top.hzy520.stock.service.StockService;

import java.util.List;
import java.util.Map;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 17:13 2018/6/8
 */

@RestController
@RequestMapping("/stock")
public class StockController {
    @Autowired
    @Qualifier("stockServiceImpl")
    private StockService stockService;
    /**
    *@Author: HouZeYu
    *@Description:
    *@Date: 20:25 2018/6/8
    */
    @RequestMapping("/query/{id}")
    public ApiResult queryStock(@PathVariable("id") Long id){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"查询成功");
        int Count=stockService.queryStock(id);
        Stock stock=new Stock();
        stock.setSkuId(id);
        stock.setStock(Count);
        apiResult.setData(stock);
        return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 初始化商品redis库存
    *@Date: 21:43 2018/6/8
    */
    @RequestMapping("/initStock/{id}")
    public ApiResult initStock(@PathVariable("id") Long id){
        stockService.initStock(id);
     return new ApiResult(Constants.RESP_STATUS_OK,"初始化成功");
    }
    /**
    *@Author: HouZeYu
    *@Description: 扣减库存
    *@Date: 8:18 2018/6/9
    */
    @RequestMapping("/reduce")
    public ApiResult<Map<Long,Integer>> reduceStock(@RequestBody List<ReduceStock> reduceStockList){
      ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"库存扣减成功");
      Map<Long,Integer> result= stockService.reduceStock(reduceStockList);
      apiResult.setData(result);
       return apiResult;
    }
}
